package com.wy.controller;

import com.wy.common.BaseCommonController;
import com.wy.common.exception.BizException;
import com.wy.constant.RedisConst;
import com.wy.constant.SpringMVCConst;
import com.wy.domain.AreaCity;
import com.wy.domain.AreaCounty;
import com.wy.domain.AreaProvince;
import com.wy.protocol.ERRORCODE;
import com.wy.service.IAreaCityService;
import com.wy.service.IAreaCountyService;
import com.wy.service.IAreaProvinceService;
import com.wy.util.AreaUtil;
import com.wy.util.RedisUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@Scope(SpringMVCConst.SCOPE)
@RequestMapping("/area")
public class AreaController extends BaseCommonController {

	private final Logger LOGGER= LoggerFactory.getLogger(AreaController.class);

	@Autowired
	private IAreaProvinceService provinceServiceImpl;

	@Autowired
	private IAreaCityService cityServiceImpl;

	@Autowired
	private IAreaCountyService countyServiceImpl;
	
	@RequestMapping(value = "/findProvince", method = RequestMethod.GET)
	public String findProvince() throws BizException {
		Object result = null;
		try {
			if(!RedisUtil.getInstance().exists(RedisConst.AREA_PROVINCE)){
				List<AreaProvince> lists =  provinceServiceImpl.findAll();
				new AreaUtil().setAreaProvince(lists);
			}
			result = RedisUtil.getInstance().get(RedisConst.AREA_PROVINCE);
		}catch(Exception e){
			LOGGER.info("====wy /area/findProvince PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		if(result==null){
			LOGGER.info("====wy /area/findProvince PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		return result.toString();
	}
	
	@RequestMapping(value = "/findCity", method = RequestMethod.GET)
	public String findCity(@RequestParam(value="provinceId",required=false)Long provinceId) throws BizException {
		Object result = null;
		String key = RedisConst.AREA_CITY+provinceId;
		try {
			if (!RedisUtil.getInstance().exists(key)) {
				List<AreaCity> lists =  cityServiceImpl.findAll();
				new AreaUtil().setAreaCity(provinceId,lists);
			}
			result = RedisUtil.getInstance().get(key);
		}catch(Exception e){
			LOGGER.info("====wy /area/findCity PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		if(result==null){
			LOGGER.info("====wy /area/findCity PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		return result.toString();
	}
	
	@RequestMapping(value = "/findCounty", method = RequestMethod.GET)
	public String findCounty(@RequestParam(value="cityId",required=false)Long cityId) throws BizException {
		Object result = null;
		String key = RedisConst.AREA_COUNTY+cityId;
		try {
			if (!RedisUtil.getInstance().exists(key)) {
				List<AreaCounty> lists =  countyServiceImpl.findAll();
				new AreaUtil().setAreaCounty(cityId,lists);
			}
			result = RedisUtil.getInstance().get(key);
		}catch(Exception e){
			LOGGER.info("====wy /area/findCounty PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		if(result==null){
			LOGGER.info("====wy /area/findCounty PARAM_ERROR ");
			throw new BizException(ERRORCODE.FAIL.getCode(), ERRORCODE.FAIL.getMessage());
		}
		return result.toString();
	}
	
}